import Vue from 'vue';
import App from './App';
import router from './router';
import "@/assets/css/default.css";
import 'lib-flexible/flexible.js';
// import '@/untils/http.js';
import '@/untils/configUrl.js';
import {post, get} from '@/untils/https.js';
import store from './store'

// import VConsole from 'vconsole'
// const vConsole = new VConsole();

//定义全局变量
Vue.prototype.$post = post;
Vue.prototype.$get = get;
Vue.prototype.$eventHub = new Vue();

Vue.config.productionTip = false;
let WsClient = false;  //当前ws协议是否确认连接人，路由跳转不重新确认
let active_id = localStorage.getItem('active_id');
//后台确认连接ws信息 读取active_id 通过ws发送给后台
function wsClient() {
    if (!WsClient) {
        console.log("后台确认连接人信息");
        let aid = localStorage.getItem('active_id');
        active_id = aid;
        let sendData = {
            cmd: 'join',
            active_id: aid
        };
        Vue.prototype.$webstock.send(JSON.stringify(sendData));
        WsClient = true;
    }
}


//全局路由钩子
router.beforeEach((to, from, next) => {
    if (to.meta.wsClient === false) {
        //登录注册页面  没有active_id不需要向后台发送确认信息
        console.log("当前是登录注册页面");
        next();
        return
    }
    
    if (!WsClient) {
        //登录过后的页面，有active_id向后台发送确认信息
        Vue.prototype.$eventHub.$on("openSuccess", (res) => {
            console.log("握手成功");
            //异步执行
            wsClient();
        });
    }
    
    
    next();
});


export const VueObj = new Vue({
    el: '#app',
    router,
    store,
    ...App,
    template: '<App/>'
});


let wsUrl = "ws://cs.api.org:9527";

//ws初始化
function initWs() {
    let webstock = new WebSocket(wsUrl);
    Vue.prototype.$webstock = webstock;
    webstock.onopen = function () {
        VueObj.$eventHub.$emit("openSuccess")
    };
    
    webstock.onmessage = function (message) {
        VueObj.$eventHub.$emit("receive", message)
    };
    
    // webstock.onclose = function (event) {
    //     console.log("websocket 断开连接");
    //     initWs();//断开连接重连
    // };
    
    webstock.onerror = function (event) {
        webstock.close(event);
        console.log("websocket error");
    };
}

//刷新页面执行
//通过ws与后台确认连接关系
initWs();

if (!VueObj.$store.getActive_id){
    VueObj.$store.dispatch("initActive_id",localStorage.getItem("active_id"));
}
//接收后台信息推送
Vue.prototype.$eventHub.$on("receive", (res) => {
    let data = JSON.parse(res.data);
    if (data.cmd === "ping") {
        Vue.prototype.$webstock.send(JSON.stringify({cmd: "pong"}));
        return
    }
    if (data.cmd === "status"){
        return
    }
    if (data.cmd === "user" || data.cmd === "user_group" || data.cmd === "status"){
        VueObj.$store.dispatch("newChatRecord",data);
    }
    
    /*active_id: "gnYx5VyPRMKkZwX2Bm89YeTFDvVS-xYnt7eueUU9jG8"
    active_image: "uploads/images/2019042653565297.jpg"
    cmd: "user"
    messageId: "194"
    msg: "123"
    send_user_id: "gnYx5VyPRMKkZwX2Bm89YWy49MYrpZy9G3JFqeL8Q_g"
    time: "1556433777"
    type: "1"
    user_remark: ""
    */
    
    
});
